<?php
class App_Tpl_Form_Field_Image extends App_Tpl_Form_Field{

	public function PreparePost($fieldPostData, $params)
	{
		if (!is_array($fieldPostData) || !isset($fieldPostData['file']))
		{
			$fieldPostData['file'] = '';
		}
		
		$fieldPostData['file'] = preg_replace(array('#^' . preg_quote(_ROOT_URL) . '#is', '#^' . preg_quote(_ROOT_URL_STATIC) . '#is'), '', urldecode($fieldPostData['file']));
		
		$outValue = array();
		if (is_file(_ROOT_PATH . $fieldPostData['file']))
		{
			$size = @getimagesize(_ROOT_PATH . $fieldPostData['file']);
			if ($size[0] && $size[1])
			{
				$outValue = array('file' => $fieldPostData['file'] , 'width' => $size[0] , 'height' => $size[1]);
			}
		}

		return $outValue;
	}
	
	public function PrepareOut($fieldData, $params)
	{
		$thumbFolder = Mozg_Mapper::Get('Image')->GetImageThumbFolder('mediathumbnail');
		
		$fieldData = (array)$fieldData;
		$fieldData = array(
                                'file' => $fieldData['file'] ? _ROOT_URL_STATIC. (string)$fieldData['file'] : '',
								'thumbnail' => $fieldData['file'] ? _ROOT_URL_STATIC.$thumbFolder. (string)$fieldData['file'] : '',
                                'thumbPath'	=> _ROOT_URL_STATIC.$thumbFolder,
								'width' => (int)$fieldData['width'],
								'height' => (int)$fieldData['height'],
		);
		
		return $fieldData;
	}
	
    protected function RenderField()
    {
    	return $this->render('form/field/_image.phtml');
    }

}